
format ms coff


include 'inc\win32\win32a.inc'

;include 'inc\lib\strlib.inc'

include 'src\fasm\version.inc'



extrn '_printf' as printf:dword
extrn '_strlen' as strlen:dword

extrn '_Print' as Print:dword
extrn '_Error' as Error:dword

extrn '_getMem_C' as getMem:dword
extrn '_regetMem_C' as regetMem:dword
extrn '_freeMem_C' as freeMem:dword
extrn '_cpyMem_C' as cpyMem:dword
extrn '_sizeMem_C' as sizeMem:dword

;extrn '_getLuaVar@12' as getLuaVar:dword ;L,varName,type
;extrn '_setLuaVar@16' as setLuaVar:dword ;L,varName,type,value

;extrn _fncs:dword
;extrn _fncs_size:dword

;extrn '_GlobalMemoryStatus@4' as GlobalMemoryStatus:dword

;extrn '_GetEnvironmentVariableA@12' as GetEnvironmentVariable:dword

;extrn '_GetStdHandle@4' as GetStdHandle:dword


extrn '_CreateThread@24' as CreateThread:dword
;extrn '_SetThreadPriority@8' as SetThreadPriority:dword
;extrn '_ExitThread@4' as ExitThread:dword


extrn '_CreateFileA@28' as CreateFile:dword
extrn '_ReadFile@20' as ReadFile:dword
extrn '_WriteFile@20' as WriteFile:dword
extrn '_SetFilePointer@16' as SetFilePointer:dword
extrn '_CloseHandle@4' as CloseHandle:dword
extrn '_GetFullPathNameA@16' as GetFullPathName:dword
extrn '_SetCurrentDirectoryA@4' as SetCurrentDirectory:dword


extrn '_GetTickCount@0' as GetTickCount:dword

extrn '_GetSystemTime@4' as GetSystemTime:dword

extrn '_CharLowerA@4' as CharLower:dword



extrn '_module_get@8' as module_get:dword
extrn '_module_check@8' as module_check:dword
extrn '_module_member_new@20' as module_member_new:dword
extrn '_module_member_get@12' as module_member_get:dword
extrn '_module_memb_convert_type@4' as module_memb_convert_type:dword
;extrn '_module_memb_set_fixe@8' as module_memb_set_fixe:dword
extrn '_module_memb_set_type@8' as module_memb_set_type:dword
extrn '_module_memb_set_postfixe@16' as module_memb_set_postfixe:dword
extrn '_module_access_func_set_fixe@16' as module_access_func_set_fixe:dword
extrn '_module_origin_quest_set_fixe@12' as module_origin_quest_set_fixe:dword
extrn '_module_interface_create@8' as module_interface_create:dword


MR_OK           =       IDOK
MR_ABORT        =       IDABORT



public FasmCompile as "_fasmCompile@16"
;public fasm as "_fasmCompile"



;uglobal
;  comp_handle dd ?
;endg


section '.text' code readable executable


;include 'inc\lib\strlib.asm'
include 'src\fasm.asm'


;uglobal
;  include 'src\fasm\variable.inc'
;endg


;fasm:
;       stdcall InitCompiler
;
;       ret ;4*1

;----------------------------------------------------------------------------------------------
proc prstr, .str
begin
	pusha
	
	push [.str]
	push .shablon
	call printf
	add esp,4*2

	popa
	
	return
.shablon db "> %s",10,13,0
endp

proc prhex, .hex
begin
	pusha
	
	push [.hex]
	push .shablon
	call printf
	add esp,4*2

	popa
	
	return
.shablon db "> %x",10,13,0
endp


szFunc_to_compile_input_file db "@",0

proc ProgressWndStepIt
begin
        return
endp


public code_start as '_code_start'
public code_size as '_code_size'
public current_module as '_current_module'
;public _my_get_code_start
;_my_get_code_start:	;fixeOffset,fixe
;	mov eax,[code_start]
;ret


proc FasmCompile, .lpszModName, .lpszToCompileFunc, .lplpResultingCode, .lpCodeSize
;.exit dd ?
.comp_handle dd ?
begin

pusha


;call strlen


;stdcall prstr,[.lpszToCompileFunc]

;        stdcall InitStrings
;        stdcall InitCompiler

;	call InitStrings

;pusha
;stdcall prstr, [.lpszToCompileFunc]
;popa

mov eax,[.lplpResultingCode]
mov [lpCompiledCode],eax

mov eax,[.lpCodeSize]
mov [lpCodeSize],eax

	xor eax,eax
	mov esi,[.lpszToCompileFunc]
	mov [output_file],esi
@@:
	dec ah
	jz .wrongformat ;invalid_file_format;
	lodsb
	test al,al
	jz .wrongformat ;invalid_file_format;
	cmp al,'!'
	jnz @b

	mov [lpszFunc_to_compile],esi
	mov [pos_in_func_to_compile],esi
	dec esi
	mov byte [esi],0


	call InitCompiler



;push eax
;stdcall prstr, [pos_in_func_to_compile]
;pop eax

push [lpszFunc_to_compile]
call strlen
mov [size_of_func_to_compile],eax
pop eax
;	push ecx
;	push edi
;
;	xor ecx,ecx
;	dec ecx
;	mov edi,eax
;	mov al,0
;	cld
;	repnz scasb
;	not ecx
;	dec ecx
;	mov [size_of_func_to_compile],ecx
;	stdcall prhex, [size_of_func_to_compile]
;
;	pop edi
;	pop ecx

mov eax,szFunc_to_compile_input_file
mov [input_file],eax
;mov eax,[.lpszModName]
;mov [output_file],eax

        mov     [.comp_handle], 0

;        invoke  SendMessage, [frmMsg], MWM_CLEARMESSAGES, 0, 0

; Set compiler properties...
;        mov     eax, [poMemory]
;        cmp     eax, 8192               ; 8MEG is smallest possible memory
;        jge     @f
        mov     eax, 8192
;@@:
        mov     [compiler_memory], eax

; Set output filename...

;        xor     eax, eax
;        cmp     [poBinNameAuto], FALSE
;        jne     .setoutputname
;
;        stdcall StrLen, [poBinName]
;        test    eax, eax
;        jz      .setoutputname


;uglobal
;  out_filename_buffer rb 256
;endg

 ;       lea     ecx, [eax+1]
;        stdcall StrPtr, [poBinName]
;        mov     esi, eax
;        mov     edi, out_filename_buffer
;        rep movsb
;
;        mov     eax, out_filename_buffer
;
;.setoutputname:
;        mov     [output_file], eax

;        stdcall ProgressWndCreate, 7, 1, cProgressCaption,0

;        invoke  SendMessage, [hProjManager], PMM_GETMAINFILE, 0, 0
;        test    eax, eax
;        jnz     .docompile

;        mov     eax, [ptrCurrentFile]
;        test    eax, eax
;        jz      .finish

;        cmp     [eax+TOpenFile.ptrType], ftCommonSource
;        jne     .finish

.docompile:
;	push szFunc_to_compile_input_file
;stdcall DisplayBlock,szFunc_to_compile_input_file
;	stdcall PrepareForCompilation,szFunc_to_compile_input_file

	stdcall PrepareForCompilation, szFunc_to_compile_input_file ;[eax+TOpenFile.hFileName]
;push szFunc_to_compile_input_file
;call prstr

        test    eax, eax
        jz      .outofmemory

;                lea eax,[.comp_handle]
;        invoke  CreateThread, NULL, $4000, flat_assembler, NULL, NULL, eax ;.comp_handle
;        mov     [.comp_handle], eax
xor eax,eax
mov [current_module],eax
stdcall module_check,0,[output_file]
mov [current_module],eax

		push 0
		call flat_assembler

;        stdcall ShowModal, ebx, MSF_CENTER
;        mov     [.exit], eax
;
;        cmp     [.exit], MR_CANCEL
;        jne     .compended
;
;        invoke  TerminateThread, [.comp_handle], -1

;.compended:
;        stdcall CleanupAfterCompilation

        call CleanupAfterCompilation

mov eax,[lpszFunc_to_compile]
dec eax
mov byte [eax],'!'

;        cmp     [.exit], MR_OK
;        je      .finalize

;; find the error message.
;        invoke  SendMessage, [frmMsg], MWM_FIRSTERROR, 0, 0

.finalize:
;        invoke  CloseHandle, [.comp_handle]

;        stdcall ProgressWndDestroy
;        mov     eax, [.exit]

;        stdcall FreeStrings

;        call FreeStrings

popa
	mov eax,1
        return

.outofmemory:
;        invoke  MessageBox, [hApplication], cOutOfMemory, NULL, MB_ICONERROR or MB_OK
;        mov     eax, MR_ABORT

;        stdcall FreeStrings

;        call FreeStrings

popa
	xor eax,eax
        return

.wrongformat:
;        mov     eax, MR_ABORT

;        stdcall FreeStrings

;        call FreeStrings

popa
	xor eax,eax
        return

.finish:
;        mov     eax, MR_ABORT

;        stdcall FreeStrings

;        call FreeStrings

popa
        return
endp

;uglobal
;  comp_handle dd ?
;endg

;iglobal
;    cProgressCaption db 'Compiling...',0
;    cOutOfMemory db "Can't allocate memory for compilator. Check settings of Fresh.", 0
;endg











;uglobal
;  case_table   rb 100h
;endg







section '.data' data readable writeable
;iglobal
;szFunc_to_compile_input_file db "@",0
;       hfile           dd 0
;endg

;iglobal
;  cTotalLines    db  'Total lines:', 0
;  cAsmLines      db  'Binary code lines: ', 0

;  cCompileTime db 'Total time: ',0
;  cPreprocessTime db 'Preprocessing: ', 0
;  cParsingTime    db 'Parsing:', 0
;  cCapturingTime  db 'Creating labels tree: ', 0
;  cAssemblingTime db 'Assembling time: ', 0
;  cLastPassTime   db 'Additional pass time:', 0
;  cValuesTime     db 'Capturing values: ', 0
;  cFormatTime     db 'Formating time: ',0

;  cTimeUnits   db ' seconds',0
;  cCompilePasses db 'Passes: ', 0
;  cCompileSize   db 'Compiled: ', 0
;  cSizeUnits     db ' bytes',0
;  cSummary       db 'Compilation summary:', 0
;endg

;iglobal
  cMsgPreprocess db 3, 'Preprocessing...',13,0
  cMsgParsing    db 3, 'Parsing...',13,0
  cMsgAssembling db 3, 'Assembling...',13,0
  cMsgFormating  db 3, 'Formating...',13,0

  cLMiddleBracket db ' [',0
  cRMiddleBracket db ']',0

  cErrorPrefix   db 'Error: ', 0
;endg





section '.bss' data readable writeable

current_module	dd ?
tmp_module	dd ?
tmp_mod_memb	dd ?
tmp_type	dd ?
fixe_id		dd ?
;uglobal
  include 'src\fasm\variable.inc'
;endg

;uglobal
;  strDisplay dd ?
;  iLastIcon  dd ?
;  hLastMsg   dd ?
;endg

 hStrFileName   dd ?

;
; Fresh internal variables not related to FASM compiler
;uglobal
  CompiledFileName      dd ?
;  ptrIniPath            dd ?

  param_buffer          rd 10h
  path_buffer           rb 1000h

  hfile                 dd ?

  error_data_size       dd ?
  compiler_memory       dd ?

  ptrMemoryAllocated    dd ?

  save_stack_pointer    dd ?
;endg

;uglobal
  bytes_count           dd ?
  systime               SYSTEMTIME

;  start_time            dd ?
;  preprocess_time       dd ?
;  parsing_time          dd ?
;  capturing_time        dd ?
;  assembling_time       dd ?
;  last_pass_time        dd ?
;  values_time           dd ?
;
;  total_time            dd ?

  exit_code             dd ?
  buffer                rb 1000h
;endg


;uglobal
;  StrTable        TStrTable      ; StrLib library variable.
;endg







;uglobal
;  case_table   rb 100h
;endg



;uglobal
;       ESP_storage     dd ?
        lpszFunc_to_compile dd ?
        lpCompiledCode dd ?
        lpCodeSize dd ?

        size_of_func_to_compile dd ?
        pos_in_func_to_compile dd ?

;       param_buffer    rd 10h
;       hthread         dd ?
;       hmem_display    dd ?
 ;
;       compiler_memory dd ?
;       compiler_priority dd ?
 ;
;       bytes_count dd ?
 ;
;       hmem_error_data dd ?
;
 ;
;       systime SYSTEMTIME
 ;
  ;
;; to delete ???
;       path_buffer     rb 4000h
 ;
;endg


